package com.cskaoyan.service.impl;

import com.cskaoyan.bean.CskaoyanmallCollect;
import com.cskaoyan.bean.CskaoyanmallCollectExample;
import com.cskaoyan.bean.bo.WXCollectAODBO;
import com.cskaoyan.bean.vo.WXCollectListByPageVO;
import com.cskaoyan.bean.vo.WXCollectListVO;
import com.cskaoyan.mapper.CskaoyanmallCollectMapper;
import com.cskaoyan.service.WXCollectService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WXCollectServiceImpl implements WXCollectService {

    @Autowired
    CskaoyanmallCollectMapper collectMapper;

    @Override
    public WXCollectListVO list(Integer type, Integer page, Integer limit) {
        WXCollectListVO wxCollectListVO = new WXCollectListVO( );
        List<WXCollectListByPageVO> wxCollectListByPageVOS = collectMapper.selectWXCollect(1);
        PageInfo<WXCollectListByPageVO> wxCollectListByPageVOPageInfo = new PageInfo<>(wxCollectListByPageVOS);
        long total = wxCollectListByPageVOPageInfo.getTotal( );
        wxCollectListVO.setTogalPages(total);
        wxCollectListVO.setCollectList(wxCollectListByPageVOS);
        return wxCollectListVO;
    }

    @Override
    public String addordelete(WXCollectAODBO wxCollectAODBO) {

        Integer userId = 1;
        Integer type = wxCollectAODBO.getType( );
        Integer valueId = wxCollectAODBO.getValueId( );

        CskaoyanmallCollectExample collectExample = new CskaoyanmallCollectExample( );
        collectExample.createCriteria( ).andValueIdEqualTo(valueId).andUserIdEqualTo(userId);
        List<CskaoyanmallCollect> collectList = collectMapper.selectByExample(collectExample);
        if (collectList == null || collectList.size( ) == 0) {
            CskaoyanmallCollect collect = new CskaoyanmallCollect( );
            collect.setUserId(userId);
            collect.setValueId(valueId);
            collect.setDeleted(false);
            collectMapper.insertSelective(collect);
            return "add";
        } else if (collectList.get(0).getDeleted( )) {
            CskaoyanmallCollect collect = new CskaoyanmallCollect( );
            collect.setDeleted(false);
            collectMapper.updateByExampleSelective(collect, collectExample);
            collectMapper.insertSelective(collect);
            return "add";
        } else {
            CskaoyanmallCollect collect = new CskaoyanmallCollect( );
            collect.setDeleted(true);
            collectMapper.updateByExampleSelective(collect, collectExample);
            collectMapper.insertSelective(collect);
            return "delete";
        }
    }
}